package cn.com.homeyoda.aimate.aihealth.ui.health;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;

import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.GlideLoadUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.FamilyMembersBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityChronicDiseaseBinding;
import cn.com.homeyoda.aimate.aihealth.manager.UserInfoManager;
import cn.com.homeyoda.aimate.aihealth.ui.health.frragment.ChronicDiseaseAddFragment;
import cn.com.homeyoda.aimate.aihealth.ui.health.frragment.ChronicDiseaseFragment;
import cn.com.homeyoda.aimate.aihealth.ui.health.viewModel.ChronicDiseaseViewMode;
import cn.com.homeyoda.aimate.aihealth.ui.my.MyActivity;
import cn.com.homeyoda.aimate.aihealth.views.MemberChangeDialog;

/**
 * 慢性病管理
 */
public class ChronicDiseaseActivity extends BaseActivity<ActivityChronicDiseaseBinding, ChronicDiseaseViewMode> {

    private MemberChangeDialog memberChangeDialog;
    private ChronicDiseaseFragment chronicDiseaseFragment = new ChronicDiseaseFragment();
    private ChronicDiseaseAddFragment chronicDiseaseAddFragment = new ChronicDiseaseAddFragment();

    public void changeChronicDiseaseFragment() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.framelayout, chronicDiseaseFragment) // 添加到容器
                .commit();
    }

    public void changeChronicDiseaseAddFragment() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.framelayout, chronicDiseaseAddFragment) // 添加到容器
                .commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        changeChronicDiseaseFragment();
    }

    @Override
    public ChronicDiseaseViewMode initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(ChronicDiseaseViewMode.class);
    }

    @Override
    public void initData() {
        super.initData();

        initView();
        viewModel.initUserInfo();
    }

    private void initView() {

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        binding.tvTitle.setText(R.string.chronic_disease);

        binding.btnChangeMember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewModel.planUserStateLiveData.getValue() == null) {
                    ToastUtils.showLong("成员未初始化，请稍后重试");
                    return;
                }

                if (memberChangeDialog == null) {
                    memberChangeDialog = new MemberChangeDialog(ChronicDiseaseActivity.this, viewModel.familyMembers, viewModel.planUserStateLiveData.getValue().getId()
                            , new MemberChangeDialog.SelectClickListener() {
                        @Override
                        public void onSelectMember(FamilyMembersBean.DataBean.FamilyMembers member) {
                            viewModel.changeMember(member);
                        }
                    });
                } else {
                    memberChangeDialog.setDatas(viewModel.familyMembers, viewModel.planUserStateLiveData.getValue().getId());
                }

                memberChangeDialog.show();
            }
        });

        binding.btnAddMember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(MyActivity.class);
                finish();
            }
        });


    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public ChronicDiseaseViewMode getViewModel() {
        return viewModel;
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.planUserStateLiveData.observe(this, userBean -> {
            binding.setPlanUserBean(userBean);
            GlideLoadUtils.loadImg(binding.crvHead, userBean.getAvatar(), R.mipmap.ic_member1);
            viewModel.queryFaceInfo();
        });

        viewModel.planUserStateLiveData.stateEnumMutableLiveData.observe(this, state -> {
            Log.d("pangtao","stateEnumMutableLiveData state = " + state);
            if (state == StateLiveData.StateEnum.Error) {
                binding.clNotUser.setVisibility(View.VISIBLE);
                binding.clUserInfo.setVisibility(View.INVISIBLE);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.framelayout.setVisibility(View.INVISIBLE);
            } else if (state == StateLiveData.StateEnum.Success){
                binding.framelayout.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                binding.clNotUser.setVisibility(View.INVISIBLE);
                binding.clUserInfo.setVisibility(View.VISIBLE);
            }
        });


        viewModel.faceInfoBeanStateLiveData.observe(this, faceInfoBean -> {
            if (UserInfoManager.getInstance().chronicDiseaseMap.size() > 0) {
                changeChronicDiseaseFragment();
            } else {
               changeChronicDiseaseAddFragment();
            }

            viewModel.initChronicDisease();
            viewModel.setTitleSelectPosition(viewModel.titleSelectPosition);
            viewModel.initSelectData();

        });


        if (chronicDiseaseFragment != null) {
            chronicDiseaseFragment.initViewObservable();
        }

        if (chronicDiseaseAddFragment != null){
            chronicDiseaseAddFragment.initViewObservable();
        }

    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_chronic_disease;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


}
